package com.zr.daoimpl;


import java.util.List;

import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.zr.dao.DistributionpriceDaoLPN;
import com.zr.model.Distributionprice;
import com.zr.model.Distributionrangeprice;
import com.zr.model.User;
import com.zr.vo.Drp_dpVO;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Repository("disDao")
public class DistributionpriceDaoImplLPN  extends BaseDaOImpl implements DistributionpriceDaoLPN{

	@Override
	public JSONArray getDis(int page, int rows, int uid) {
		JSONArray j=new JSONArray();

		String hql ="select dis.disId,dis.disFirstKilogramPrice,dis.disNextKilogramPrice,dis.disFirstCubicPrice,dis.disNextCubicPrice,dis.disRemarks,dis.disState, dis.deliverypointByDpIdf.dpName,dis.deliverypointByDpIds.dpName,dis.ordertype.otName  from Distributionprice  dis ";
				
		//where dis.deliverypointByDpIdf.employeeinformations.user.uid=:uid
		Query query = this.getCurrentSession().createQuery(hql);
		//query.setInteger("uid", uid);
		
			List<Object[]> diss = query.list(); 
		for (Object[] objects : diss) {
			JSONObject jo = new JSONObject();
			
			jo.put("disId", objects[0]);
			jo.put("disFirstKilogramPrice", objects[1]);
			jo.put("disNextKilogramPrice", objects[2]);
			jo.put("disFirstCubicPrice", objects[3]);
			jo.put("disNextCubicPrice", objects[4]);
			jo.put("disRemarks", objects[5]);
			jo.put("disState", objects[6]);
			jo.put("dpNamef", objects[7]);
			jo.put("dpNames", objects[8]);
			jo.put("otName", objects[9]);
			j.add(jo);
		}
		query.setFirstResult((page-1)*rows);
		query.setMaxResults(rows);

		return j;
	}

	
	@Override
	public int getDisCount() {
		String hql = "select count(dis.disId) from Distributionprice  dis";
    	Query query = this.getCurrentSession().createQuery(hql);
    	int count = ((Number) query.uniqueResult()).intValue();
		return count;
	}
	
	
	@Override
	public boolean insertDIS(Double disFirstKilogramPrice, Double disNextKilogramPrice, Double disFirstCubicPrice,
			Double disNextCubicPrice, String disRemarks, String disState, int dpIdf, int dpIds, int otId) {
		boolean flag = false;
		String sql = "insert into distributionprice(disFirstKilogramPrice,disNextKilogramPrice,disFirstCubicPrice,disNextCubicPrice,disRemarks,disState,dpIdf,dpIds,otId) "
				+ "values(:disFirstKilogramPrice,:disNextKilogramPrice,:disFirstCubicPrice,:disNextCubicPrice,:disRemarks,:disState,:dpIdf,:dpIds,:otId)";
		Query query = this.getCurrentSession().createSQLQuery(sql);
		query.setDouble("disFirstKilogramPrice",disFirstKilogramPrice);
		query.setDouble("disNextKilogramPrice", disNextKilogramPrice);
		query.setDouble("disFirstCubicPrice", disFirstCubicPrice);
		query.setDouble("disNextCubicPrice", disNextCubicPrice);
		query.setString("disRemarks", disRemarks);
		query.setString("disState", disState);
		query.setInteger("dpIdf", dpIdf);
		query.setInteger("dpIds", dpIds);
		query.setInteger("otId", otId);
		int i = query.executeUpdate();
		if (i>0) {
			flag = true;
		}
		return flag;
	}

	@Override
	public int updateDIS(int disId, Double disFirstKilogramPrice, Double disNextKilogramPrice,
			Double disFirstCubicPrice, Double disNextCubicPrice, String disRemarks) {
		Distributionprice dis = (Distributionprice) this.getCurrentSession().get(Distributionprice.class, disId);
		dis.setDisFirstKilogramPrice(disFirstKilogramPrice);
		dis.setDisNextKilogramPrice(disNextKilogramPrice);
		dis.setDisFirstCubicPrice(disFirstCubicPrice);
		dis.setDisNextCubicPrice(disNextCubicPrice);
		dis.setDisRemarks(disRemarks);
		int i = dis.getDisId();
		return i;
	}

	@Override
	public int finddisIdByDpidfAndDpidsAndOtid(int dpIdf, int dpIds, int otId) {
		int disId = 0;
		try {
			String hql = "from Distributionprice  dis  where dis.deliverypointByDpIdf.dpId=:dpIdf and dis.deliverypointByDpIds.dpId=:dpIds and dis.ordertype.otId=:otId";
			
			Query query = this.getCurrentSession().createQuery(hql);
			query.setInteger("dpIdf", dpIdf);
			query.setInteger("dpIds", dpIds);
			query.setInteger("otId", otId);	
			
			List<Distributionprice>  dis = query.list();
			disId=dis.get(0).getDisId();
			
			
		} catch (IndexOutOfBoundsException e) {
			// TODO: handle exception
			//System.out.println("出现异常");
		}
		return disId;
		
	}



}
